package com.hm.common.interceptor;


import com.google.gson.Gson;
import com.hm.business.vo.ResultVO;

import com.hm.common.config.AuthConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* @Description: 权限拦截器
* @param
* @return
* @throws
* @author Coder编程
* @date 2021/6/16 18:41
*/
@Component
public class AuthInterceptor implements HandlerInterceptor {


    @Autowired
    private AuthConfig authConfig;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String jwtToken = request.getHeader("jwtToken");
        boolean contains = authConfig.getAuthMap().containsKey(jwtToken);
        if (!contains) {
            ResultVO<String> tokenError = new ResultVO(404,"身份验证失败!");
            response.getWriter().println(new Gson().toJson(tokenError));
            response.setContentType("application/json");
        }
        return contains;
    }


}
